package com.djmw.mouse.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import com.djmw.mouse.entity.Application;
import com.djmw.mouse.entity.CvLang;
import com.djmw.mouse.entity.MemberUser;
import com.djmw.mouse.entity.Position;
import com.djmw.mouse.repository.ApplicationDao;

//Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly = true)
public class ApplicationService {

    @Autowired
    private ApplicationDao applicationDao;

    public Page<Application> getApplicationByMemberUserId(Long memberUserId, int pageNumber, int pageSize) {
        PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, new Sort(Direction.DESC, "createDate"));
        Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
        filters.put("pk.memberUser.id", new SearchFilter("pk.memberUser.id", Operator.EQ, memberUserId));
        Specification<Application> spec = DynamicSpecifications.bySearchFilter(filters.values(), Application.class);
        return applicationDao.findAll(spec, pageRequest);
    }

    public Page<Application> getApplicationByPositionId(Long positionId, Map<String, Object> searchParams,
            int pageNumber, int pageSize, String sortType, String sortDir) {
        PageRequest pageRequest = ServiceUtil.buildPageRequest(pageNumber, pageSize, sortType, sortDir, "createDate:d");
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        filters.put("pk.position.id", new SearchFilter("pk.position.id", Operator.EQ, positionId));
        Specification<Application> spec = DynamicSpecifications.bySearchFilter(filters.values(), Application.class);
        return applicationDao.findAll(spec, pageRequest);
    }

    public Application findExistingApplication(Position position, CvLang cvLang, MemberUser user) {
        return applicationDao.findExistingApplicaiton(position.getId(), cvLang.getId(), user.getId());
    }

    public Page<Application> getApplicationByCompanyId(Long companyId, Map<String, Object> searchParams,
            int pageNumber, int pageSize, String sortType, String sortDir) {
        PageRequest pageRequest = ServiceUtil.buildPageRequest(pageNumber, pageSize, sortType, sortDir, "createDate:d");
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        filters.put("pk.position.company.id", new SearchFilter("pk.position.company.id", Operator.EQ, companyId));
        Specification<Application> spec = DynamicSpecifications.bySearchFilter(filters.values(), Application.class);
        return applicationDao.findAll(spec, pageRequest);
    }
}
